package com.strawcom.charmring.imagebrowser;

import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;
import com.strawcom.charmring.BaseActivity;
import com.strawcom.charmring.C;
import com.strawcom.charmring.R;
import com.strawcom.charmring.view.TitleView;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

public class ImagesActivity extends BaseActivity implements OnItemClickListener{
	private GridView mGridView;
	private ArrayList<String> list;
	private ChildAdapter adapter;
	private TitleView titleView;
	
	private HashMap<String, ArrayList<String>> mGruopMap = new HashMap<String, ArrayList<String>>();
	private final static int SCAN_OK = 1;
	private ProgressDialog mProgressDialog;
	
	@Override protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_images);
		super.onCreate(savedInstanceState);
		
		mGridView = (GridView) findViewById(R.id.child_grid);
//		list = getIntent().getStringArrayListExtra("data");
//		adapter = new ChildAdapter(this, list, mGridView);
//		mGridView.setAdapter(adapter);
		titleView=(TitleView) findViewById(R.id.title_view);
		titleView.setTitleText(R.string.my_photo);
		mGridView.setOnItemClickListener(this);
		
		getImages();
	}

	@Override public void onItemClick(AdapterView<?> arg0, View view, int poistion, long id) {
		Intent intent=new Intent(this,ShowImageActivity.class);
		intent.putStringArrayListExtra("list", list);
		intent.putExtra("poistion", poistion);
		startActivity(intent);
	}

	/**
	 * 利用ContentProvider扫描手机中的图片，此方法在运行在子线程中
	 */
	private void getImages() {
		if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
			Toast.makeText(this, R.string.no_sd_card, Toast.LENGTH_SHORT).show();
			return;
		}
		
		mGruopMap.clear();
		
		//显示进度条
		mProgressDialog = ProgressDialog.show(this, null, "正在加载...");
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
				ContentResolver mContentResolver = getContentResolver();

				//只查询jpeg和png的图片
				Cursor mCursor = mContentResolver.query(mImageUri, null,
						MediaStore.Images.Media.MIME_TYPE + "=? or "
								+ MediaStore.Images.Media.MIME_TYPE + "=?",
						new String[] { "image/jpeg", "image/png" }, MediaStore.Images.Media.DATE_MODIFIED);
				
				while (mCursor.moveToNext()) {
					//获取图片的路径
					String path = mCursor.getString(mCursor
							.getColumnIndex(MediaStore.Images.Media.DATA));
					
					//获取该图片的父路径名
					String parentName = new File(path).getParentFile().getName();

					
					//根据父路径名将图片放入到mGruopMap中
					if (!mGruopMap.containsKey(parentName)) {
						ArrayList<String> chileList = new ArrayList<String>();
						chileList.add(path);
						mGruopMap.put(parentName, chileList);
					} else {
						mGruopMap.get(parentName).add(path);
					}
				}
				
				mCursor.close();
				
				//通知Handler扫描图片完成
				mHandler.sendEmptyMessage(SCAN_OK);
				
			}
		}).start();
		
	}
	
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		mHandler.removeMessages(SCAN_OK);
	}
	
	private Handler mHandler = new Handler(){
		@Override public void handleMessage(Message msg) {
			switch (msg.what) {
			case SCAN_OK:
				//关闭进度条
				mProgressDialog.dismiss();
				
				// 找到戒指相册
				if(mGruopMap.size() == 0){
					showToast(R.string.no_photo);
					finish();
				}else{
					ArrayList<String> childList = mGruopMap.get(C.app.album_name);
					if(childList == null || childList.size() < 1) {
						showToast(R.string.no_photo);
						finish();
					} else {
						Collections.reverse(childList); 
						list = childList;
						adapter = new ChildAdapter(ImagesActivity.this, list, mGridView);
						mGridView.setAdapter(adapter);
					}
				}
				
				break;
			}
		}
	};
}
